package com.ziyu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ziyu.api.system.entity.SysOperLog;
import com.ziyu.common.core.utils.StringUtils;
import com.ziyu.system.mapper.SysOperLogMapper;
import com.ziyu.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 操作日志 服务层处理
 *
 * @author cyj
 */
@Service
public class SysOperLogServiceImpl implements ISysOperLogService {

    @Autowired
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     * @return 结果
     */
    @Override
    public int insertOperlog(SysOperLog operLog) {
        return operLogMapper.insert(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();
        // 相似：参数1为true条件有效，下同
        wrapper.like(StringUtils.isNotEmpty(operLog.getTitle()), SysOperLog::getTitle, operLog.getTitle());
        // 相等
        wrapper.eq(StringUtils.isNotEmpty(operLog.getBusinessType()), SysOperLog::getBusinessType, operLog.getBusinessType());
        // 在...之中
        wrapper.in(StringUtils.isNotEmpty(operLog.getBusinessTypes()), SysOperLog::getBusinessType, operLog.getBusinessTypes());
        wrapper.eq(StringUtils.isNotEmpty(operLog.getStatus()), SysOperLog::getStatus, operLog.getStatus());
        wrapper.like(StringUtils.isNotEmpty(operLog.getOperName()), SysOperLog::getOperName, operLog.getOperName());
        // 大于等于
        wrapper.ge(StringUtils.isNotEmpty(operLog.getParams().get("beginTime")), SysOperLog::getOperTime, operLog.getParams().get("beginTime"));
        // 小于等于
        wrapper.le(StringUtils.isNotEmpty(operLog.getParams().get("endTime")), SysOperLog::getOperTime, operLog.getParams().get("endTime"));
        // 排序
        wrapper.orderByDesc(SysOperLog::getOperId);
        return operLogMapper.selectList(wrapper);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds) {
        return operLogMapper.deleteBatchIds(Arrays.asList(operIds));
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId) {
        return operLogMapper.selectById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        operLogMapper.cleanOperLog();
    }
}
